// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.api.ads.dfp.lib.factory;

import com.google.api.ads.common.lib.factory.FactoryConfiguration;
import com.google.api.ads.common.lib.soap.ProductFrameworkModule;
import com.google.api.ads.dfp.lib.DfpProductFrameworkModule;
import com.google.api.ads.dfp.lib.client.DfpSession;
import com.google.common.annotations.VisibleForTesting;

import javax.annotation.Nullable;

/**
 * Utility class to create DFP service clients. The session is stored
 * as a reference within the client so that the session may be altered to
 * affect the behavior of the client.
 * <p>
 * Usage example:
 * <pre><code>
 * DfpSession session = new DfpSession.Builder()...build();
 * InventoryServiceInterface inventoryService =
 *    new  DfpServices().get(session, InventoryServiceInterface.class);
 * campaignService.makeRequest(...);
 * ...
 * session.setNetworkCode(...);
 * campaignService.makeRequest(...);
 * </code></pre>
 *
 * <p>
 * Implementation is thread-safe.
 * </p>
 *
 * @author Adam Rogal
 * @deprecated use {@link com.google.api.ads.dfp.axis.factory.DfpServices} or
 *             {@link com.google.api.ads.dfp.jaxws.factory.DfpServices} instead.
 */
@Deprecated
public final class DfpServices {

  private final DfpServiceClientFactory factory;

  /**
   * Default constructor.
   */
  public DfpServices() {
    this(new FactoryConfiguration.Builder().buildDefault());
  }

  /**
   * Constructor that takes a factory.
   * @param factory the factory to create services from.
   */
  @VisibleForTesting
  DfpServices(DfpServiceClientFactory factory) {
    this.factory = factory;
  }

  /**
   * Constructor that takes a factory configuration.
   * @param factoryConfiguration the factory configuration
   */
  public DfpServices(@Nullable FactoryConfiguration factoryConfiguration) {
    if (factoryConfiguration == null || factoryConfiguration.getDfpSoapModule() == null) {
      factory = new DfpServiceClientFactory(
          ProductFrameworkModule.determineSoapModule(DfpProductFrameworkModule.class));
    } else {
      factory =
          new DfpServiceClientFactory(factoryConfiguration.getDfpSoapModule());
    }
  }

  /**
   * Gets a client for the service represented by the interface with a reference
   * to the session.
   *
   * @param <T> the service type
   * @param dfpSession the DFP session
   * @param interfaceClass the service interface class
   * @return the client for the service
   */
  public <T> T get(DfpSession dfpSession, Class<T> interfaceClass) {
    return factory.getServiceClientAsInterface(dfpSession, interfaceClass);
  }
}
